<template>
  <div class="course-container">
    <el-input placeholder="搜索分类" style="width:500px" clearable v-model="filterText">
      <el-button slot="append" icon="el-icon-search" @click="getTreeList()"></el-button>
      <template slot="prepend">
        <el-button  type="primary" icon="el-icon-plus" @click="btnSave(1)">添加</el-button>
      </template>
       
    </el-input>
      <el-tree
          class="filter-tree"
          :data="treeData"
          :props="defaultProps"
          default-expand-all
          @current-change="currentChange"
          :filter-node-method="filterNode"
          ref="tree">
          <div class="custom-tree-node" slot-scope="{ node, data }">
            <span>{{ node.label }}</span>
            <span>
              <el-link
                type="primary"
                size="mini"
                class="el-icon-plus"
                @click="() => append(data)">
                新增
              </el-link>
              <el-link style="padding:0 10px"
                type="success"
                size="mini"
                class="el-icon-edit"
                @click="() => update(data)">
                编辑
              </el-link>
               <el-link style="padding:0 10px"
                type="warning"
                size="mini"
                class="el-icon-sort"
                @click="() => onmove(data)">
                移动
              </el-link>
              <el-link 
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="() => remove(data)">
                删除
              </el-link>
            </span>
           </div>
      </el-tree>

      <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="500px">
          <el-form :model="ruleForm"  size="medium" :rules="rules" ref="ruleForm" label-width="90px" >
            <el-form-item label="上级分类" v-if="maxLabel"> 
              {{maxLabel}}
            </el-form-item>
            <el-form-item label="分类名称" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入分类名称" maxlength="30"></el-input>
            </el-form-item>
          </el-form>
         <span slot="footer" class="dialog-footer">
            <el-button  size="medium" icon="el-icon-close" @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" icon="el-icon-finished"  
               size="medium" @click="submitForm('ruleForm')">确 定</el-button>
          </span>
    </el-dialog>
    <el-dialog
        title="当前移动"
        :visible.sync="moveVisible"
        width="500px">
          <el-form :model="ruleForm"  size="medium" :rules="rules" ref="ruleForm" label-width="90px" >
            <el-form-item label="分类名称"> 
              {{maxLabel}}
            </el-form-item>
            <el-form-item label="选择分类" prop="parentCategoryId">
               <el-cascader style="width:100%"
                v-model="ruleForm.parentCategoryId" 
                :options="treeData"
                :props="{
                  children: 'children',
                  label: 'name',
                  checkStrictly: true,
                  value:'categoryId'
                }"></el-cascader> 
            </el-form-item>
          </el-form>
         <span slot="footer" class="dialog-footer">
            <el-button  size="medium" icon="el-icon-close" @click="moveVisible = false">取 消</el-button>
            <el-button type="primary" icon="el-icon-finished"  
               size="medium" @click="submitMoveForm('ruleForm')">确 定</el-button>
          </span>
    </el-dialog>
    </div>
</template>
<script>
import {getCategoryTree,saveCategoryTree,delCategoryTree,moveCategoryTree} from '@/api/teachManage'
export default {
  name:'course',
  data() {
    return {
      filterText:'',
       treeData:[],
       maxLabel:'',
        defaultProps: {
          children: 'children',
          label: 'name',
          value:'categoryId'
        },
      ruleForm: {parentCategoryId:0,name:'',categoryId:''},
       rules: {
        name: [ { required: true, message: '请输入分类名称', trigger: 'blur' }],
        parentCategoryId: [ { required: true, message: '请选择分类', trigger: 'blur' }]
      },
      dialogVisible: false,
      moveVisible:false,
      chooseData:null,
      title:''
    }
  },
  watch: {
      filterText(val) {
        this.$refs.tree.filter(val);
      }
    },
  computed: {},
  created() {
    this.getTreeList()
  },
  methods: {
    getChildren(list){
      if(list.length){
       return  list.map((item)=>{
          if(item.children.length){
            item.children= this.getChildren(item.children)
          }else{
            item.children =null
          }
        return item
       })
      }else{
        return []
      }
    },
    getTreeList(){
      getCategoryTree({
        "limit": 999,
        "name": this.filterText,
        "orderField": "",
        "orderType": "",
        "page": 1
      }).then((res)=>{
        if(res.code===0){
          this.treeData = this.getChildren(res.data)
        }
      })
    },
    filterNode(value, data) {
        if (!value) return true;
        return data.name.indexOf(value) !== -1;
    },
    append(data) {
        this.ruleForm.categoryId = null
        this.ruleForm.parentCategoryId = data.categoryId
        this.maxLabel=data.name
        this.ruleForm.name=''
        this.title ='新增'
        this.dialogVisible=true
      },
      update(data){
        this.ruleForm.parentCategoryId = 0
        this.ruleForm.categoryId=data.categoryId
        this.ruleForm.name=data.name
        this.maxLabel=''
        this.title ='编辑'
        this.dialogVisible=true
      },
      onmove(data){
        this.ruleForm.categoryId=data.categoryId
        this.maxLabel=data.name
        this.moveVisible=true
      },
      remove(data) {
        this.$confirm('您确定要删除该数据？', '消息提示', {
        }).then(() => {
          delCategoryTree([data.categoryId]).then((res)=>{
            if (res.code == 0) {
                this.$message.success("删除成功");
                this.getTreeList()
              } else {
                this.$message.error(res.message||"操作失败");
              }
          })
        }).catch(action => {
        });
      },
      submitMoveForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            moveCategoryTree({
              categoryId:this.ruleForm.categoryId,
              parentCategoryId:this.ruleForm.parentCategoryId[this.ruleForm.parentCategoryId.length-1]
            }).then((res) => {
              if (res.code == 0) {
                this.$message.success("移动成功");
                this.moveVisible = false;
                this.getTreeList()
              } else {
                this.$message.error(res.message||"操作失败");
              }
            })
          }
        })
    },
      btnSave(type){
        this.ruleForm.parentCategoryId=0
        this.title ='新增'
        this.ruleForm.name=''
        this.maxLabel=''
        this.dialogVisible=true
      },
      currentChange(item){
        this.chooseData =item
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            saveCategoryTree(this.ruleForm).then((res) => {
              if (res.code == 0) {
                this.$message.success("操作成功");
                this.dialogVisible = false;
                this.getTreeList()
              } else {
                this.$message.error(res.message||"操作失败");
              }
            })
          }
        })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style lang="scss">
.course-container {
  padding: 25px;
  box-sizing: border-box;
  height: calc(100vh - 84px);
  overflow: auto;
  .filter-tree{
    margin-top:20px;
    width:500px;
  }
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding:10px 0;
  }
}
</style>
